package tgc.rj.garbage;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import android_serialport_api.SerialPort;
import tgc.rj.garbage.custom.IConstant;

public class CanyuActivity extends AppCompatActivity implements View.OnClickListener, Runnable {
    private Button btn_clear, btn_ma;
    private TextView tv_text;
    public SerialPort serialPort = null;
    public InputStream inputStream = null;
    public OutputStream outputStream = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canyu);
        btn_ma = findViewById(R.id.btn_ma);
        btn_clear = findViewById(R.id.btn_clear);
        btn_ma.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        tv_text = findViewById(R.id.tv_text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ma:
                beginRead();
                break;
            case R.id.btn_clear:
                tv_text.setText(null);
                break;
        }
    }

    public void beginRead() {
        openSrialPort();
        new Thread(this).start();
    }

    public void openSrialPort() {
        try {
            serialPort = new SerialPort(new File("/dev/" + IConstant.PORT2), IConstant.BAUDRATE2, 0);
            //获取打开的串口中的输入输出流，以便于串口数据的收发
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        byte[] readData = new byte[1024];
        byte[] x = new byte[1024];
        int idx = 0;
        for (int j = 0; j < 2; j++) {
            try {
                try {
                    Thread.sleep(300);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                int size = inputStream.read(readData);
                for (int i = 0; i < size; i++) {
                    x[idx++] = readData[i];
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        String str = new String(x, 0, idx);
        Message message = new Message();
        Bundle bundle = new Bundle();
        bundle.putString("data", str);
        message.setData(bundle);
        handler.sendMessage(message);
        closeSerialPort();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String str = msg.getData().getString("data");
            tv_text.setText(tv_text.getText().toString() + str + "\n");
        }
    };

    public void closeSerialPort() {
        Log.i("test", "关闭串口");
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            serialPort.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
